package cn.jbt.plugin.util;

import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCacheManager;

@SuppressWarnings("all")
public class RedisCacheManagerUtil {

    private RedisCacheManager redisCacheManager;

    public void setRedisCacheManager(RedisCacheManager redisCacheManager) {
        this.redisCacheManager = redisCacheManager;
    }

    /**
     * <h2>获取缓存信息</h2>
     */
    public <T> T getCache(String cacheName, String key) {

        Cache cache = redisCacheManager.getCache(cacheName);
        if (cache == null) {
            return null;
        }
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper == null) {
            return null;
        }
        return (T) valueWrapper.get();
    }

    /**
     * <h2>添加缓存信息</h2>
     */
    public void putCache(String cacheName, String key, Object value) {
        Cache cache = redisCacheManager.getCache(cacheName);
        if (cache != null) {
            cache.put(key, value);
        }
    }

    /**
     * <h2>清理缓存</h2>
     */
    public void evictCache(String cacheName, String key) {
        Cache cache = redisCacheManager.getCache(cacheName);
        if (cache != null) {
            cache.evict(key);
        }
    }

    /**
     * <h2>清理缓存</h2>
     */
    public void clearCache(String cacheName) {
        Cache cache = redisCacheManager.getCache(cacheName);
        if (cache != null) {
            cache.clear();
        }
    }


}
